package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.Login;
import com.example.yuejian.yuejian.model.UserModel;

public class LoginActivity extends AppCompatActivity {
        private EditText et_telephone;
        private  EditText et_password;
        private CheckBox cb_remember;
        private Button btn_login;
    private TextView tv_forgetpwd;
    int userid;
    int status;
    int clubid;
    private SharedPreferences sp;

;
    private ModleListener<Login> modleListener=new ModleListener<Login>() {
        @Override
        public void onResponse(Login l) {
            Login res=l;
            userid=res.getData().getId();
            clubid=res.getData().getClub_id();
            status=res.getRetCode();
            if(0==status)
            {
                //失败处理
                Toast.makeText(LoginActivity.this, res.getRetMsg(), Toast.LENGTH_SHORT).show();
            } else
            {
                SaveUser(userid,clubid);
                //登录成功处理
                Intent intnet =new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intnet);
                Toast.makeText(getApplication(), "登陆成功", Toast.LENGTH_SHORT).show();
                finish();
            }


        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplication(),"fail:"+msg,Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        readSp();
    }
    private  void initViews()
    {
        sp=this.getSharedPreferences("UserInfo", MODE_PRIVATE);
        btn_login=(Button)findViewById(R.id.btn_login_confirm);
        et_telephone=(EditText)findViewById(R.id.et_login_phone);
        et_password=(EditText)findViewById(R.id.et_login_password);
        cb_remember=(CheckBox)findViewById(R.id.remember_password);
        tv_forgetpwd=(TextView)findViewById(R.id.tv_forget_password);

    }
    public void initEvents()
    {
        tv_forgetpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }
  public   void login(View view)
    {


        UserModel userModel=new UserModel();
        userModel.userLogin(et_telephone.getText().toString(),et_password.getText().toString(),modleListener);

    }
    public  void register(View view)
    {
        Intent intnet =new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intnet);
    }
    public void SaveUser(int userid,int clubid){
        final SharedPreferences sharedPreferences=getSharedPreferences("UserInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();//获取编辑器
        editor.putString("user_id", userid+"");
        editor.putString("club_id", clubid+"");
        editor.putString("telephone", et_telephone.getText().toString());
        editor.putString("userpass", et_password.getText().toString());
        //存开关状态
        editor.putBoolean("remember", cb_remember.isChecked());
        editor.commit();
    }
    private void readSp() {
        // TODO Auto-generated method stub
        //获取用户名
        String strname=sp.getString("telephone", null);
        //获取密码
        String strpass=sp.getString("userpass", null);
        //获取开关状态
        Boolean remember=sp.getBoolean("remember",false);
        if(remember)
        {
            cb_remember.setChecked(true);
            et_telephone.setText(strname);
            et_password.setText(strpass);

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1 && resultCode==RESULT_OK){
            String username=data.getStringExtra("username");
            et_telephone.setText(username);
        }
    }


 }
